#!/bin/bash

work_dir=$PWD
REPO_NAME=$work_dir/_logs/repos.txt
user=""	# 需自行指定gitee上要推送到的用户名

DATE=$(date +%y%m%d)
LOGFILE=$work_dir/_logs/$DATE.txt
touch $LOGFILE

echo "$(date +%H%M) sync start" >> $LOGFILE

for i in $(cat $REPO_NAME)
do
    # echo $i
    cd $i
    git remote rm origin-gitee
    git fetch origin	# if new branch created, we can get it.
    for brname in `git branch -r | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`
    do
        git checkout $brname
        git reset --hard HEAD
        git clean -df
	git pull origin $brname
    done
    if [ $? -ne 0 ]; then echo "  $i" >> $LOGFILE; fi
    git remote add origin-gitee git@gitee.com:$user/$i.git
    git push --all origin-gitee 

    cd $work_dir
done

echo "$(date +%H%M) sync terminal" >> $LOGFILE
